from flask import Flask, render_template, template_rendered
from contextlib import contextmanager

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html', items=[1]*10, name="world")

@contextmanager
def captured_templates(app):
    recorded = []
    def record(sender, template, context, **extra):
        print('sender:', sender)
        print('template:', template)
        print('context:', context)
        recorded.append((template, context))
    template_rendered.connect(record, app)
    try:
        yield recorded
    finally:
        template_rendered.disconnect(record, app)

with captured_templates(app) as templates:
    rv = app.test_client().get('/')
    assert rv.status_code == 200
    assert len(templates) == 1
    template, context = templates[0]
    assert template.name == 'index.html'
    assert len(context['items']) == 10

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8000, debug=True)